<template>
  <div class="dialog">
    <el-dialog :title="'部门' + titleText" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules"   label-width="120px" class="demo-ruleForm">
        <el-form-item label="部门名称 ：" prop="departmentName">
          <el-input v-model="formData.departmentName" placeholder="请输入部门名称" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import homeBaseApi from "@/api/basic-info-manage/homeBase";
import DepartmentApi from "@/api/basic-info-manage/department";

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
    userList: {
      type: [Array],
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      formData: {},
      rules: {
        departmentName: [
          { required: true, message: "请输入部门名称", trigger: ["change", "blur"] }
        ],
      },
      // 可选基地
      baseHomeList: [],
      showDialog: this.show,
    };
  },

  watch: {
    show (v) {
      this.showDialog = v;
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    },
  },
  created () {
    this.queryBaseHome()
  },
  mounted () {
  },
  methods: {
    // 查询可选择基地
    queryBaseHome () {
      let params = {
        page: 1,
        size: 10000
      }
      homeBaseApi.queryPageByCondition(params).then(res => {
        if (res.success) {
          this.baseHomeList = res.data.content
        }
      });
    },
    // 删除改行
    deleteRow (index, rows) {
      rows.splice(index, 1);
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let params = { ...this.formData };
            DepartmentApi.edit(params).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,修改失败");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let params = { ...this.formData };
            DepartmentApi.add(params).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else {
                this.$message.error("系统错误,添加失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-table th {
  color: #000;
  background-color: rgba(120, 182, 193, 0.4) !important;
}

::v-deep .el-table .el-table__row--striped {
  color: #000;
  background-color: rgba(186, 186, 186, 0.4) !important;
}

::v-deep.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group,
.el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table__empty-text {
  color: #000;
}

::v-deep .el-table {
  color: #000;
}

.addInfo {
  width: 100%;
  margin: 10px;
  background-color: #237FC3;
  color: #fff;
  font-size: 18px;

}

.el-form {
  margin-top: 10px;
}
</style>
